package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.ChefDuelData;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Spoon;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuel2;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuelActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_CHEF_DUEL2_JOIN_TABLE, desc = "参加宴会")
public class ChefDuel2JoinTableCmd extends AbstractCommand {

	//本服的活动
	int activityId;
	long homeUserId;
	int tableId;
	int spoonId;
	//可赴宴的活动
	int otherActivityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2JoinTableReqMsg reqMsg = ChefDuelProto.ChefDuel2JoinTableReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		homeUserId = reqMsg.getUserId();
		tableId = reqMsg.getTableId();
		spoonId = reqMsg.getSpoonId();
		if(reqMsg.hasOtherActivityId()){
			otherActivityId = reqMsg.getOtherActivityId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!ChefDuelActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
			return;
		}
		ChefDuel2Spoon cookBoy2Spoon = ChefDuelActivityMgr.getChefDuel2Spoon(activityId,spoonId);
		if(cookBoy2Spoon == null){
			ret = GameErrorCode.E_CHEF_DUEL2_SPOON_NOT_FOUND;
			return;
		}
		if(!player.getModule(ChefDuelModule.class).isAble(activityId)){
            ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_ABLE;
            return;
        }
		if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cookBoy2Spoon.getConsume()))){
			player.getModule(PreReduceModule.class).preReduce(cookBoy2Spoon.getConsume());
		}else{
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		if(otherActivityId > 0 && otherActivityId != activityId){
			//赴宴范围的试吃
			OtherActivityChefDuel2 otherActivityChefDuel2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(otherActivityId);
			if(otherActivityChefDuel2 != null){
				ChefDuelData duelData = ChefDuelMgr.getUserChefDuel2DataNoLock(activityId, player);
				ChefDuelMgr.getLogger().info("玩家{}，试吃赴宴范围活动 {}，玩家{}， 桌子{}，菜品 {}", player.getUserId(), otherActivityId, homeUserId, tableId, spoonId);
				ret = otherActivityChefDuel2.sendJoinMsg(player.getUserId(),homeUserId,tableId,spoonId, activityId, duelData.getCampId());
				if(ret == 0){
					//赴宴范围的场景
					otherActivityChefDuel2.addSceneUser(player.getUserId(), homeUserId);
				}
			}
		}else {
			ret = ChefDuelMgr.sendJoinMsg(activityId,player.getUserId(),homeUserId,tableId,spoonId, activityId);
			ChefDuelMgr.addChefDuel2SceneUser(activityId, player.getUserId(), homeUserId);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel2JoinTableRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2JoinTableRespMsg.newBuilder();
			respMsg.addAllForbidSpoonId(player.getModule(ChefDuelModule.class).getForbidSpoonIdList(activityId));
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL2_JOIN_TABLE, respMsg);
		}
	}
}
